#ifndef _ZKRD522_H_
#define _ZKRD522_H_
#include <stdio.h>
#include <string.h>

#define NFC_DEBUG 0

//配置CRC初始值
#define CRC_A                   0x01
#define CRC_B                   0x03

#define BIT0   (1<<0)
#define BIT1   (1<<1)
#define BIT2   (1<<2)
#define BIT3   (1<<3)
#define BIT4   (1<<4)
#define BIT5   (1<<5)
#define BIT6   (1<<6)
#define BIT7   (1<<7)

//通信速率
#define RATE_106                   0x0
#define RATE_212                   0x1
#define RATE_424                   0x2
#define RATE_848                   0x3
/**
 * PCD命令字
 ****************************************************************
 */
//命令集
#define PCD_IDLE              	0x00        //取消当前命令
#define PCD_AUTHENT           	0x14        //验证密钥
#define PCD_RECEIVE           	0x16        //接收数据
#define PCD_TRANSMIT          	0x1A        //发送数据
#define PCD_TRANSCEIVE        	0x1E        //发送并接收数据
#define PCD_RESETPHASE        	0x3F        //复位
#define PCD_CALCCRC           	0x12        //CRC计算
#define PCD_M1STOP            	0x15

//寄存器地址宏定义
//page0
#define command_reg             0x01
#define interupen_reg           0x02        //中断使能寄存器
#define irqpinconfig_reg        0x03        //配置IRQ引脚值
#define interup_reg             0x04        //中断标志位寄存器
#define divirq_reg              0x05        //中断标志位寄存器
#define error_reg               0x06        //错误寄存器
#define status1_reg             0x07        //状态1寄存器
#define status2_reg             0x08        //状态2寄存器
#define fifodata_reg            0x09
#define fifolevel_reg           0x0a        //fifo长度寄存器地址，6bit，清FIFO
#define waterlevel_reg          0x0b        //waterlevel
#define ctl_reg                 0x0c        //控制寄存器，置位数字系统
#define mcurst_reg              0x0d        //软复位寄存器
#define collpos_reg             0x0e

//page1 
#define crcpreset_reg           0x11        //CRC初始值寄存器
#define txmode_reg              0x12
#define rxmode_reg              0x13
#define txcontrol_reg           0x14
#define snd_bit_num_reg         0x15        //发送比特数寄存器
#define snd_byte_num_reg        0x16        //发送字节数寄存器
#define waitparam_reg           0x17
#define rec_begin_bit_pos_reg   0x18        //接收起始到第八bit作为一个字节，防冲突使用
#define	receive_byte_num_l_reg  0x19        //接收数据大小低8位寄存器
#define	receive_byte_num_h_reg 	0x1a        //接收数据大小第9位寄存器
#define recstate_reg            0x1b
#define fdt_reg             		0x1d        //FDT
#define typebset_reg            0x1e
#define uart_baud_reg  		    	0x1f        //配置uart波特率地址

//page2
#define crcmsb_reg              0x21        //crc结果高8位
#define crclsb_reg              0x22        //crc结果低8位
//模拟前端寄存器
#define svl_reg                 0x23
#define svhsvl_reg              0x24
#define	analogen_reg						0x25
#define	rxgain_reg							0x26
#define n_cwconductance_reg     0x27
#define cwconductance_reg       0x28
#define modconductance_reg      0x29
#define tmode_reg               0x2a        //定时器模式选择，预分频器高4位在此寄存器的低4位
#define tprescaler_l            0x2b        //预分频器低8位
#define treload_h               0x2c        //计数重装载值高8位
#define treload_l               0x2d        //计数重装载值低8位
#define tcounter_h              0x2e        //定时器当前值高8位
#define tconnter_l              0x2f        //定时器当前值低8位

//page3
#define vrxampi_probe_reg       0x30
#define muxsel_vl_reg						0x31		    //数字IO的选择控制端
#define muxsel_vh_reg						0x32		    //数字IO的选择控制端
#define	testpinen_reg						0x33		    //数字IO的输入输出控制
#define testpinvalue_reg				0x34		    //数字IO的控制以及IO值
#define analogtest_reg					0x38		    //模拟IO配置
#define codersvr_reg            0x39				//模拟前端配置
#define codersvl_reg            0x3a				//模拟前端配置

//LPCD寄存器
#define ex_reg                  0x0F
#define lpcd_ctr1_reg           0x01
#define lpcd_clkcalib_reg				0x02
#define lpcd_txctr_reg          0x03
#define lpcd_cwctr_reg          0x04
#define lpcd_ncwctr_reg         0x05
#define lpcd_analogen_reg       0x06
#define lpcd_rxgain_reg					0x07
#define lpcd_t1_reg             0x08
#define lpcd_t2_reg             0x09
#define lpcd_t3_reg             0x0a
#define lpcd_autowukup_reg      0x0b
#define lpcd_thresholdhi_reg		0x0c
#define lpcd_thresholdlo_reg		0x0d
#define lpcd_set_reg            0x0e
#define lpcd_irq_reg            0x0f
#define vmidbdtime_reg					0x11
#define fastdetect_reg					0x12
#define sensitivity_reg					0x13
#define lpcd_value_reg          0x14
#define test_adcconf_reg        0x15
#define clk_conf_reg        		0x16
#define other_ctrl_reg        	0x17
#define tprescaler_hi_reg				0x1c
#define tprescaler_lo_reg				0x1d

/**
 * Mifare Error Codes
 * Each function returns a status value, which corresponds to
 * the mifare error
 * codes.
 ****************************************************************
 */
#define MI_OK                           0

#define MI_CRC_NOTZERO                  1
#define MI_COLL                         2

#define MI_NOTAGERR                     (-1)
#define MI_CHK_FAILED                   (-1)
#define MI_CRCERR                       (-2)
#define MI_CHK_COMPERR                  (-2)
#define MI_EMPTY                        (-3)
#define MI_AUTHERR                      (-4)
#define MI_PARITYERR                    (-5)
#define MI_CODEERR                      (-6)
#define MI_SERNRERR                     (-8)
#define MI_KEYERR                       (-9)
#define MI_NOTAUTHERR                   (-10)
#define MI_BITCOUNTERR                  (-11)
#define MI_BYTECOUNTERR                 (-12)
#define MI_IDLE                         (-13)
#define MI_TRANSERR                     (-14)
#define MI_WRITEERR                     (-15)
#define MI_INCRERR                      (-16)
#define MI_DECRERR                      (-17)
#define MI_READERR                      (-18)
#define MI_OVFLERR                      (-19)
#define MI_POLLING                      (-20)
#define MI_FRAMINGERR                   (-21)
#define MI_ACCESSERR                    (-22)
#define MI_UNKNOWN_COMMAND              (-23)
#define MI_COLLERR                      (-24)
#define MI_RESETERR                     (-25)
#define MI_INITERR                      (-25)
#define MI_INTERFACEERR                 (-26)
#define MI_ACCESSTIMEOUT                (-27)
#define MI_NOBITWISEANTICOLL            (-28)
#define MI_QUIT                         (-30)
#define MI_INTEGRITY_ERR                (-35) //完整性错误(crc/parity/protocol)
#define MI_ERR_PARAM                    (-36)  //参数错误
#define MI_ERR_TRANSMIT                 (-37)  //发送错误
#define MI_ERR_RECEIVE                  (-38)  //接收错误
#define MI_ERR_PPS                      (-39)

#define MI_RECBUF_OVERFLOW              (-50)
#define MI_SENDBYTENR                   (-51)
#define MI_SENDBUF_OVERFLOW             (-53)
#define MI_BAUDRATE_NOT_SUPPORTED       (-54)
#define MI_SAME_BAUDRATE_REQUIRED       (-55)
#define MI_WRONG_PARAMETER_VALUE        (-60)
#define MI_BREAK                        (-99)
#define MI_NY_IMPLEMENTED               (-100)
#define MI_NO_MFRC                      (-101)
#define MI_MFRC_NOTAUTH                 (-102)
#define MI_WRONG_DES_MODE               (-103)
#define MI_HOST_AUTH_FAILED             (-104)
#define MI_WRONG_LOAD_MODE              (-106)
#define MI_WRONG_DESKEY                 (-107)
#define MI_MKLOAD_FAILED                (-108)
#define MI_FIFOERR                      (-109)
#define MI_WRONG_ADDR                   (-110)
#define MI_DESKEYLOAD_FAILED            (-111)
#define MI_WRONG_SEL_CNT                (-114)
#define MI_WRONG_TEST_MODE              (-117)
#define MI_TEST_FAILED                  (-118)
#define MI_TOC_ERROR                    (-119)
#define MI_COMM_ABORT                   (-120)
#define MI_INVALID_BASE                 (-121)
#define MI_MFRC_RESET                   (-122)
#define MI_WRONG_VALUE                  (-123)
#define MI_VALERR                       (-124)
#define MI_COM_ERR                     (-125)
#define PROTOCOL_ERR                    (-126)
///用户使用错误
#define USER_ERROR                      (-127)

#define MAX_TRX_BUF_SIZE    64

typedef struct transceive_buffer
{
    unsigned char mf_command;
    unsigned int mf_length;
    unsigned char mf_data[MAX_TRX_BUF_SIZE];
} xdata transceive_buffer;

void set_bit_mask(unsigned char reg, unsigned char mask);
void clear_bit_mask(unsigned char reg, unsigned char mask);
char pcd_config(unsigned char type);
void pcd_set_rate(unsigned char rate,unsigned char wait_param);
char pcd_com_transceive(struct transceive_buffer *pi);
char pcd_get_crc(unsigned char mode, unsigned char len, unsigned char *crc_src, unsigned char* crc_lsb, unsigned char* crc_msb);
#endif
